<template>
  <div class="headerSearch-wrap">
      <svg-icon
        class="search-icon"
        class-name="search-icon"
        icon="search"
        @click.stop="onShowClick"
      />
      <el-select
        ref="headerSearchSelectRef"
        :class="select"
        placeholder="输入搜索关键词"
        filterable
        remote
        default-first-option
        :remote-method="querySearch"
        @change='onSelectChange'
      >
        <el-option
         v-for="option in searchOptions"
        :key="option.item.path"
        :label="option.item.title.join(' > ')"
        :value="option.item"
        >
        </el-option>
        
      </el-select>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRouter } from 'vue-router'
import { filterRouters , generateMenus } from '@/utils/route'
import Fuse from 'fuse.js' //导入模糊查询库
import { generateRoutes } from './FuseData'
// 检索数据源
const router = useRouter()

const searchPool = computed(() => {
  const filterRouter = filterRouters(router.getRoutes())
  return generateRoutes(filterRouter)
})

// 搜索库相关
const fuse = new Fuse(searchPool.value,  {
  // 是否按优先级进行排序
  shouldSort: true,
  // 匹配算法放弃的时机，阈值0.0 需要完美匹配（字母和位置）阈值1.0匹配任何内容。
  threshold: 0.4,
  // 匹配长度唱过这个值的才会被认为是匹配的
  minMatchCharLength: 1,
  // 将被苏索的键列表，这支持嵌套路径、加权搜索，在字符串和对象数组中搜索。
  // name：搜索的键
  // weight: 对应的权重
  keys: [
    {
      name: 'title',
      weight: 0.7
    },
    {
      name: 'path',
      weight: 0.3
    }
  ]
})


// search 相关
const search = ref('')

// 搜索结果
const searchOptions = ref([])
// 搜索方法
const querySearch = (query)=> {
   if (query !== '') {
    searchOptions.value = fuse.search(query)
  } else {
    searchOptions.value = []
  }
}

const headerSearchSelectRef = ref(null)
// 展缩搜索框
const select = ref('vanishSelect')
const onShowClick = ()=> {
  select.value = select.value === 'vanishSelect' ? 'showSelect' : 'vanishSelect'
  // 光标焦点
  if (select.value === 'showSelect') {
    headerSearchSelectRef.value.focus()
  }
}
// 关闭 search 的处理时间
const onClose = () => {
  headerSearchSelectRef.value.blur()
  select.value = 'vanishSelect'
  searchOptions.value = []
}
// 监听search 打开，处理close事件
watch(select, (val) => {
  if (val === 'showSelect') {
    document.body.addEventListener('click', onClose)
  } else {
    document.body.removeEventListener('click', onClose)
  }
})




// 选中回调
const onSelectChange = (val)=> {
  router.push(val.path)
}

</script>
<style lang="scss" scoped>
@import '@/styles/variables.scss';
@import '@/styles/mixin.scss';

.headerSearch-wrap{
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 10px;
}

.search-icon{
  font-size: 20px;
  box-sizing: border-box;
  margin-right: 10px;
}

.vanishSelect{
  width: 0;
  transition: 1.2s width;
  overflow: hidden;
}
.showSelect{
  width: 200px;
  transition: .5s width;
  overflow: hidden;
  
}
::v-deep .el-input__inner{
    border-radius: 0;
    border: 0;
    padding-left: 0;
    padding-right: 0;
    box-shadow: none !important;
    vertical-align: middle;
    border-bottom: 1px solid #d9d9d9;
}
</style>